/*
    嵌套式路由
    prefix只能设置一个层级 如localhost:3000/levy
    想要设置多个父子路由 如lcoalhost:3000/levy  localhost:3000/afaart
    并访问者两个路由下面的页面
*/

const Koa = require ('koa');
const Router = require('koa-router');

//第一个子路由
const levy = new Router();
//设置子路由下面的页面路径
levy
    .get('/index', async ctx => {
        ctx.body = 'levy子路由下面的index页面';
    })
    .get('/todo', async ctx => {
        ctx.body = 'levy子路由下面的todo页面';
    });

//第二个子路由
const afaart = new Router();
//设置子路由下面的页面路径
afaart
    .get('/index', async ctx => {
        ctx.body = 'afaart子路由下面的index页面';
    })
    .get('/todo', async ctx => {
        ctx.body = 'afaart子路由下面的todo页面';
    });

//设置主路由
const router = new Router({
    prefix : '/main' //localhost:3000/main 下面的 /levy 和/afaart子路由 (localhost:3000/main/levy/index)
});
router
    .use('/levy',levy.routes(),levy.allowedMethods)
    .use('/afaart',afaart.routes(),afaart.allowedMethods);


//app调用主路由
const app = new Koa();
app
    .use(router.routes())
    .use(router.allowedMethods);

app.listen(3000, () => {
    console.log('[Server] is running at port 3000 .');
});